<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<%@page import="com.webside.points.wechat.Wechat"%>
<%@page import="com.webside.points.wechat.Constant"%> 
<%
	 //正常跳转
	String code = (String)request.getParameter("code");
  
	String openId = null;
	
	if(code != null){
		Wechat.getOpenId(code,request,response);
	}
	
	if(openId == null){
		openId = (String)request.getSession().getAttribute("openId");
	}
	//如果从其他界面返回时，code已经失效，需要重新请求一次
	
	if(openId == null){
	   response.sendRedirect("../wechat/getWeChatCode");
	   return;
	}  
	String url = Constant.REALM_URL;
	
 %> 
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link rel="stylesheet" href="../css/amazeui.min.css">
    <link rel="stylesheet" href="../css/wap.css">
    <link rel="stylesheet" href="../css/style.css"/>
	 <link rel="stylesheet" href="../css/weui.css" />
    <title>首页</title>
</head>
  <script src="../js/jquery.min.js"></script>

<script type="text/javascript" src="../js/jquery.timeago.js"></script>
 <script type="text/javascript"
	src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
		<link rel="stylesheet" href="../css/dropload.css">
	<script src="../js/dropload.min.js"></script>

    <style type="text/css">
		.bubble {
			float: left;
			display: block;
			background: #28aef5;
			line-height: 2.2rem;
			height: 2.2rem;
			border-radius: 1.1rem;
			text-align: center;
			min-width: 9rem;
			color: #fff;
			font-size: .8rem;
			text-decoration: none;
			position: relative;
			margin-right: .35rem;
		}
		.issueType{
			width:49%;
			display:inline-block;
			text-align:center;
			color:#22ac38;
			padding:10px 0px;
			border: 1px solid #e5e5e5;
		}
		.top-search{
			padding: 10px 20px;
		}
		.top-search-span{
			padding: 2px 5px;
			border-radius: 5px;
			display:inline-block;
			color:#22ac38;
			margin:5px;
		}
		.searchData{
			font-size:16px;
			padding:0px 15px;
		}
		.advertising-text{
			font-size:10px;
			border:1px solid #22ac38;
			color:#22ac38;
			border-radius:3px;
			padding:0px 2px;
		}
	</style>
    <script type="text/javascript">
	
		var interval;
	
   		$(function(){
			
			var href=window.location.href;
			
			$("#url").val(href);
			
	   		 $(".answer").each(function(){
	   			 var data=$(this).text();
	   			 
	   	        if(data.indexOf('-')!=-1){
	   	        	
					var html="<span onclick='playVoice(this)' id='"+data+"' class='bubble bubble-blue'>点击播放</span>";
					
	   	        	$(this).html(html);
	   	        }
	   	    });
			
			
			//debugger;
			
			
   		});
   		
   		function wxConfig(){
		
			var url = "${ctx }/wechat/getWxSignatureByTicket";
			
			var href=window.location.href;
			var params = {"url":href};
			   
			$.post(url, params, function(data, status, xhr){
				callbackConfig(data);
			},'json');
		}
		wxConfig();
		function callbackConfig(data) {
			 wx.config({
			      debug: false,
			      appId: data.appId,
			      timestamp: data.timestamp,
			      nonceStr: data.noncestr,
			      signature: data.signature,  
			      jsApiList: [
					'checkJsApi',
					'onMenuShareTimeline',
					'onMenuShareAppMessage',
					'hideMenuItems'
			      ]
			  }); 
		}
		
		//检查接口是否能调用
		wx.ready(function () {
			wx.checkJsApi({
			    jsApiList: [
			        'playVoice',
			        'playVoice'
			    ],
			    success: function (res) {
			        // alert(JSON.stringify(res));
			        // alert(JSON.stringify(res.checkResult.getLocation));
			        if (res.checkResult.getLocation == false) {
			            alert('你的微信版本太低，不支持微信JS接口，请升级到最新的微信版本！');
			            return;
			        }
			    }
			});
			//分享图片路径
			var url = "<%=url%>" +"images/share.jpg";
			
			//alert(url);
			
			//分享给朋友圈
			wx.onMenuShareTimeline({
				title: '欢迎使用中穆问答', // 分享标题
				link: "<%=url%>", // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				imgUrl: url, // 分享图标
				success: function () { 
					// 用户确认分享后执行的回调函数
				},
				cancel: function () { 
					// 用户取消分享后执行的回调函数
				}
			});
			//分享给朋友
			wx.onMenuShareAppMessage({
				title: '欢迎使用中穆问答', // 分享标题
				desc: '穆斯林的问题，来问中穆问答。这里有许多专家随时为您答疑解惑。', // 分享描述
				link: "<%=url%>", // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				imgUrl: url, // 分享图标
				success: function () { 
					// 用户确认分享后执行的回调函数
				},
				cancel: function () { 
					// 用户取消分享后执行的回调函数
				}
			});
		});
		
		
		$(function(){
			
			$(".issueType").click(function(){
				var issueType = $(".issueType");
				for(var i=0;i<issueType.length;i++){
					$(issueType[i]).attr("style","");
				}
				
				$(this).css("color","#969595");
				var id=$(this).attr("id");
				$('.am-list').html("");
				$(".dropload-down").remove();
				data(id);
			});
			
			data(0);
			
			function data(issueTypeId){
				// 页数
				var page = 0;
				// 每页展示多少个
				var size = 10;
				var expertId = $("#expertId").val();
				var userId = $("#userId").val();
				var linkId = $("#linkId").val();
				var imageId = $("#imageId").val();
				var titleId = $("#titleId").val();
				var describeId = $("#describeId").val();
				//var issueTypeId = $("#issueTypeId").val();
				//var user = ${user};
				//alert(userId);
				// dropload
				$('.content').dropload({
					scrollArea : window,
					loadDownFn : function(me){
						page++;
						// 拼接HTML
						var result = '';
						$.ajax({
							type: 'post',
							url: '../user/findAllUserIssue?page='+page+"&size="+size+"&issueTypeId="+issueTypeId,
							dataType: 'json',
							success: function(data){
								var arrLen = data.length;
								//alert(page);
								if(arrLen > 0){
									for(var i=0; i<arrLen; i++){
										//判断是否停用
										if(data[i].status==1){
											 var html =   '<li class="am-g am-list-item-desced pet_list_one_block" style="margin-bottom: 0px;padding:0px 20px;border-top: 10px solid #f1f1f1;">'
												
												+'<div class="pet_list_one_info" style="margin-top: 5px;">'
													+'<div class="pet_list_one_info_l">'
														+'<div class="pet_list_one_info_ico"><img src="../images/anonymity.jpg" alt=""></div>'
														+'<div class="pet_list_one_info_name">匿名用户</div>'
													+'</div>'
												+'</div>'
												+'<div class=" am-list-main">'
													+'<h3 class="am-list-item-hd pet_list_one_bt" style="color: #222;font-weight: bold;"><a href="${ctx }/issue/findById?issueId='+data[i].id+'" >'+data[i].issueContent+'</a></h3>'
												+' <div style="color:#969595;font-size:1.3rem;margin-top:6px;">'
												+'<span id="'+data[i].id+'" >姓名|工作单位职务</span><font style="float:right;"><time class="timeago" id="time'+data[i].id+'" datetime="" >0分钟前</time>首先回答</font>'
												+'</div>'
												+'<div style="color:#969595;font-size:1.1rem;border-top:1px solid #f1f1f1;margin-top:5px;padding-top:5px;margin-bottom:10px; ">'
												+'<font id="answerCount'+data[i].id+'">0人回答</font> ' 
												+'<font>'+data[i].browseNumber+'人浏览 </font>'
												+'<font id="like'+data[i].id+'">0人赞同</font> '
												+'<font id="dislike'+data[i].id+'">0人反对</font> '
												+'<font>'+data[i].rewardNumber+'人打赏</font>'
												+'<a style="float:right;font-size:1.3rem;color:#22ac38;"  href="${ctx }/issue/findById?issueId='+data[i].id+'">查看答案</a>'
												+'</div>'
												+'</div>'
											+'</li>'; 
											//alert(userId);
											 if(data[i].anonymity=='on' && data[i].userEntity.id!=userId){
													if(expertId==0){
														
													}else{
														html=html.replace("../images/anonymity.jpg",data[i].userEntity.headUrl)
														html=html.replace("匿名用户",data[i].userEntity.wechatId)
													}
												}else{
													html=html.replace("../images/anonymity.jpg",data[i].userEntity.headUrl)
													html=html.replace("匿名用户",data[i].userEntity.wechatId)
												}
											 result = result+html;
											 
											 if(i==0){
												 if(imageId!=""){
													 if(imageId.indexOf("www.saibaibu.com")!=-1){
														 var advertisingForm='<li class="am-g am-list-item-desced pet_list_one_block" style="margin-bottom: 0px;padding:0px 20px;border-bottom: 10px solid #f1f1f1;">'
																		 +'<div style="background: #fff;padding:5px 0px;">'
																		 +'<div>'+titleId+'</div>'
																		 +'<div><a href="'+linkId+'"><img src="'+imageId+'" style="width:100%;margin-top:5px;"></a></div>'
																		 +'<div> <span class="advertising-text">广告</span> </div>'
																		 +'</div>'
																		 +'</li>';
														result+=advertisingForm;
													 }else{
														 var abc =   '<li class="am-g am-list-item-desced pet_list_one_block" style="margin-bottom: 0px;padding:0px 20px;border-top: 10px solid #f1f1f1;">'
												
																+'<div class="pet_list_one_info" style="margin-top: 5px;">'
																	+'<div class="pet_list_one_info_l">'
																		+'<div class="pet_list_one_info_name">某某某公司出品</div>'
																	+'</div>'
																+'</div>'
																+'<div class=" am-list-main">'
																	+'<h3 class="am-list-item-hd pet_list_one_bt" style="color: #222;font-weight: bold;"><a href="'+linkId+'" >'+imageId+'</a></h3>'
																+' <div style="color:#969595;font-size:1.3rem;margin-top:6px;">'
																+'<font>'+describeId+'</font>'
																+'</div>'
																+'<div style="color:#969595;font-size:1.1rem;border-top:1px solid #f1f1f1;margin-top:5px;padding-top:5px;margin-bottom:10px; ">'
																+'<font class="advertising-text">广告</font> ' 
																+'<a style="float:right;font-size:1.3rem;color:#22ac38;"  href="'+linkId+'">查看详情</a>'
																+'</div>'
																+'</div>'
															+'</li>'; 
															
														result+=abc;
													 }
												 }
											}
										}
									}
														
								// 如果没有数据
								}else{
									// 锁定
									me.lock();
									// 无数据
									me.noData();
								}
								// 插入数据到页面，放到最后面
								$('.am-list').append(result);
								// 每次数据插入，必须重置
								me.resetload();
								
								$(".am-list-main span").each(function(){
								 var id=$(this).attr("id");
								 var span=$(this);
								 //alert(data);
								 var url="${ctx}/answer/findByIssueId";
									$.post(url, {"issueId":id}, function(data, status, xhr){
										callback(data);
									},'json');
									function callback(data){
										//alert(data.length);
										var like=0;
										var dislike=0;
										for(var i=0;i<data.length;i++){
											like+=data[i].likeMark;
											dislike+=data[i].dislikeMark;
										}
										//有问题
										
										
										var array = new Array();
										//遍历当前数组
										for(var i=0; i<data.length; i++){
											//如果当前数组的第i已经保存进了临时数组，那么跳过，
											//否则把当前项push到临时数组里面
											if(array.indexOf(data[i].expertId) == -1){
												array.push(data[i].expertId);
											}
										}
										
										
										$("#answerCount"+id).text(array.length+"人回答");
										
										$("#like"+id).text(like+"人赞同");
										$("#dislike"+id).text(dislike+"人反对");
										
										var answer = data[data.length-1].expertEntity;
										//alert(answer.name);
										
										$.post("${ctx}/issueType/findByIssueTypeId", {"id":answer.expertType}, function(issueType, status, xhr){
											 $(span).text(answer.name+" | "+answer.workUnit+""+answer.duty);
											 
											 $("#time"+id).attr("datetime",data[data.length-1].createDateFormat);
											 
											 $("#time"+id).timeago();
										},'json');
										
										
									}
								 
							});
							},
							error: function(xhr, type){
								//alert('Ajax error!');
								// 即使加载出错，也得重置
								me.resetload();
							}
						});
					}
				});
			}
			
			    $(function(){
					
					var $searchBar = $('#searchBar'),
						$searchResult = $('#searchResult'),
						$searchText = $('#searchText'),
						$searchInput = $('#searchInput'),
						$searchClear = $('#searchClear'),
						$searchCancel = $('#searchCancel');
					$searchResult.hide();
					function hideSearchResult(){
						$searchResult.hide();
						$searchInput.val('');
						$(".top-search").css("display","");
					}
					//清除搜索
					function cancelSearch(){
						hideSearchResult();
						$searchBar.removeClass('weui-search-bar_focusing');
						$searchText.show();
					}
					//点击搜索
					$searchText.on('click', function(){
						$searchBar.addClass('weui-search-bar_focusing');
						$searchInput.focus();
						$("#top").css("display","none");
						hideSearchResult();
					});

					//取消搜索
					$searchCancel.on('click', function(){
						cancelSearch();
						$searchInput.blur();
						$("#top").attr("style","");
						$(".top-search").css("display","none");
					});
					//小x清除
					 $searchClear.on('click', function(){
						hideSearchResult();
						$searchInput.focus();
					});	
						
				});
		});
		//键盘按下搜索
		function search(e){
			var $searchResult = $('#searchResult');
			$searchResult.show();
			$(".searchData").html('');
			$(".dropload-down").remove();
			var content = $(e).val();
			if(content.length>0){
				$(".top-search").css("display","none");
				//$("#searchResult").attr("style","margin-top:0px;");
			}else{
				$(".top-search").css("display","");
				$("#searchResult").css("display","none");
			}
			
			// 页数
			var page = 0;
			// 每页展示多少个
			var size = 10;
			//alert();
			// dropload
			$('#searchResult').dropload({
				scrollArea : window,
				loadDownFn : function(me){
					page++;
					// 拼接HTML
					var result = '';
					$.ajax({
						type: 'post',
						url: '../issue/issueSearch?page='+page+"&size="+size+"&content="+encodeURI(encodeURI(content)),
						dataType: 'json',
						success: function(data){
							if(data!=null){
								var arrLen = data.length;
								//alert(arrLen);
								if(arrLen>0){
									for(var i=0; i<arrLen; i++){
										//判断是否停用
										result += '<a href="${ctx }/issue/findById?issueId='+data[i].id+'" class="weui-cell">'
												+'<div class="weui-cell_access">'
												+'<div class="weui-cell__bd weui-cell_primary" >'
													+'<span>'+data[i].issueContent+'</span>'
												+'</div>'
											+'</div></a>';
									}
									result = result.replace(new RegExp(content, 'g'),"<font color='#22ac38'>"+content+"</font>");	
									
									//$("#searchResult").append(result);
								// 如果没有数据
								}else{
									// 锁定
									me.lock();
									// 无数据
									me.noData();
								}
							}else{
								// 锁定
								me.lock();
								// 无数据
								me.noData();
							}
							// 插入数据到页面，放到最后面
							//alert(result);
							$('.searchData').append(result);
							// 每次数据插入，必须重置
							me.resetload();
						},
						error: function(xhr, type){
							alert('Ajax error!');
							// 即使加载出错，也得重置
							me.resetload();
						}
					});
				}
			});
		}
		//关键字搜索
		function topSearch(e){
			var content = $(e).text();
			var searchInput = $("#searchInput");
			searchInput.val(content);
			search(searchInput);
			//alert(content);
		}
		function form(){
			return false;
		}
    </script>
<body>


 <div class="weui-search-bar" id="searchBar">
	<form class="weui-search-bar__form" action="#" onsubmit="return form()">
		<div class="weui-search-bar__box">
			<i class="weui-icon-search"></i>
			<input type="search" class="weui-search-bar__input" id="searchInput" onkeyup="search(this)"  placeholder="搜索问题" required/>
			<a href="javascript:" class="weui-icon-clear" id="searchClear"></a>
		</div>
		<span class="weui-search-bar__label" id="searchText">
			<i class="weui-icon-search"></i>
			<span>搜索问题</span>
		</span>
	</form>
	<a href="javascript:" class="weui-search-bar__cancel-btn" id="searchCancel">取消</a>
</div>
<div class="searchbar-result weui-cells" id="searchResult" style="margin-top:0px;">
	<div class="weui-cells  searchData" style="margin-top:0px;">

	</div>
</div>

<div class="top-search" style="display:none;">
	<div style="margin-bottom:20px;">热门搜索</div>
	<div>
		<c:forEach items="${topSearchList}" var="topSearch">
			<a href="#" onclick="topSearch(this)"><span class="top-search-span">${topSearch.name }</span></a>
		</c:forEach>
	</div>
</div>


<input type="hidden" name="url" id="url"/>
<input type="hidden" value="${userInfo.id}" id="userId"/>
<input type="hidden" value="${userInfo.expertId}" id="expertId"/>
<input type="hidden" value="${homeLine.link}" id="linkId"/>
<input type="hidden" value="${homeLine.image}" id="imageId"/>
<input type="hidden" value="${homeLine.title}" id="titleId"/>
<input type="hidden" value="${homeLine.describe}" id="describeId"/>
<div data-am-widget="gotop" class="am-gotop am-gotop-fixed">
    <a href="#top" title="">
        <img class="am-gotop-icon-custom" src="../images/goTop.png" />
    </a>
</div>

<div class="pet_mian" id="top">
  <div data-am-widget="slider" class="am-slider am-slider-a1" data-am-slider='{"directionNav":false}' >
  <ul class="am-slides">
      <li>
            <a href="${homeTop.link }"><img src="${homeTop.image }"></a>
      </li>
  </ul>
</div>


<div class="pet_content_main" style="padding:0px 0px;">
  <div data-am-widget="list_news" class="am-list-news am-list-news-default" >
  <div class="am-list-news-bd content">
	  <!--issueType-->
	 <!-- <div style="padding:0px 20px;">
		<c:forEach items="${issueTypeList }" var="issueType">
			<c:if test="${issueType.status==1 }">
				 <span class="issueType" id="${issueType.id }">
					${issueType.issueTypeName }
				 </span>
			 </c:if>
		 </c:forEach>
	  </div>-->
	  
  <ul class="am-list">

  </ul>
  </div>

    </div>

</div>
 <!--底部-->
    <div data-am-widget="navbar" class="am-navbar am-cf my-nav-footer " id="">
      <ul class="am-navbar-nav am-cf am-avg-sm-4 my-footer-ul">
		<li>
           <a href="${ctx }/user/findUserIssue?issueTypeId=0" class="">
            <span class="am-icon-home"></span>
            <span class="am-navbar-label">首页</span>
          </a>
        </li>
        <li>
          <a href="${ctx }/issueType/findAll?mark=service&openId=<%=openId %>" class="">
            <span class="am-icon-comments"></span>
            <span class="am-navbar-label">提问</span>
          </a>
        </li>
		<li>
          <a href="${ctx }/postType/findAllPostType" class="">
            <span class="am-icon-book"></span>
            <span class="am-navbar-label">学习</span>
          </a>
        </li>
        <li style="position:relative">
          <a href="${ctx }/user/findByOpenId?openId=<%=openId %>" class="">
            <span class="am-icon-user"></span>
            <span class="am-navbar-label">我的</span>
          </a>
        </li>
      </ul>
</div>
</div>
 <script src="../js/amazeui.min.js"></script>
    <script src="../js/text-calculation.js"></script>
</body>
</html>